<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Report_setoran_bank extends Authenticated_Controller{
	function __construct() {
		parent::__construct();
		$this->load->model('pencabang_model','cabang');
		$this->load->model('reservasi_model','reser');
	}
	
	function index(){
		$this->list_result();
	}
	
	function list_result($tgl_awal='',$tgl_akhir=''){
		$data['filter_cabang']=$this->cabang->get_all_cabang();
		if($tgl_awal==''){
			$data['tgl_awal']=date('m/d/Y');
		}else{
			$data['tgl_awal']=$this->date_func->change_format_date($tgl_awal,'d-m-Y','m/d/Y');
		}
		if($tgl_akhir==''){
			$data['tgl_akhir']=date('m/d/Y');
		}else{
			$data['tgl_akhir']=$this->date_func->change_format_date($tgl_akhir,'d-m-Y','m/d/Y');
		}

		$data['list_result'] = $this->reser->get_setoran_bank_join_user($tgl_awal,$tgl_akhir);
		for($i=0;$i<count($data['list_result']);$i++){
			$data['list_result'][$i]['cabang'] = $this->reser->get_setoran_by_setoran_bank($data['list_result'][$i]['id']);
		}
		$this->load->view('template/header_view');
		$this->load->view('laporan/laporan_setoran_bank',$data);
		$this->load->view('template/footer_view');
	}
	
	function cetak_pdf($tgl_awal,$tgl_akhir,$cabang){
		$data['filter_cabang']=$this->cabang->get_all_cabang();
		if($tgl_awal==''){
			$data['tgl_awal']=date('d-m-Y');
		}else{
			$data['tgl_awal']=$tgl_awal;
		}
		if($tgl_akhir==''){
			$data['tgl_akhir']=date('d-m-Y');
		}else{
			$data['tgl_akhir']=$tgl_akhir;
		}
		$data['cabang']=$cabang;
		if($cabang=='SEMUA'){
			$data['list_result'] = $this->reser->get_setoran_bank_join_user($tgl_awal,$tgl_akhir);
			for($i=0;$i<count($data['list_result']);$i++){
				$data['list_result'][$i]['cabang'] = $this->reser->get_setoran_by_setoran_bank($data['list_result'][$i]['id']);
			}
		}else{
			$data['list_result'] = $this->reser->get_setoran_bank_join_user_by_cabang($tgl_awal,$tgl_akhir,$cabang);
			for($i=0;$i<count($data['list_result']);$i++){
				$data['list_result'][$i]['cabang'] = $this->reser->get_setoran_by_setoran_bank($data['list_result'][$i]['id']);
			}
		}
		$html = $this->load->view('laporan/html_cetak_setoran_bank',$data,TRUE);
		$this->load->library('pdf');
		$this->pdf->to_pdf_landscape($html,'Report Setoran Bank.pdf');
	}
	
	function cetak_excel($tgl_awal,$tgl_akhir,$cabang){
		$this->load->library('excel');
		$data['filter_cabang']=$this->cabang->get_all_cabang();
		if($tgl_awal==''){
			$data['tgl_awal']=date('d-m-Y');
		}else{
			$data['tgl_awal']=$tgl_awal;
		}
		if($tgl_akhir==''){
			$data['tgl_akhir']=date('d-m-Y');
		}else{
			$data['tgl_akhir']=$tgl_akhir;
		}
		if($cabang=='SEMUA'){
			$data['list_result'] = $this->reser->get_setoran_bank_join_user($tgl_awal,$tgl_akhir);
			for($i=0;$i<count($data['list_result']);$i++){
				$data['list_result'][$i]['cabang'] = $this->reser->get_setoran_by_setoran_bank($data['list_result'][$i]['id']);
			}
		}else{
			$data['list_result'] = $this->reser->get_setoran_bank_join_user_by_cabang($tgl_awal,$tgl_akhir,$cabang);
			for($i=0;$i<count($data['list_result']);$i++){
				$data['list_result'][$i]['cabang'] = $this->reser->get_setoran_by_setoran_bank($data['list_result'][$i]['id']);
			}
		}
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle('title')->setDescription('description');
		$objPHPExcel->setActiveSheetIndex(0);
		//Set Keterangan
		$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Laporan Setoran Bank');
		$objPHPExcel->getActiveSheet()->setCellValue('A2', 'Perode');
		$objPHPExcel->getActiveSheet()->setCellValue('B2', $data['tgl_awal'].'-'.$data['tgl_akhir']);
		$objPHPExcel->getActiveSheet()->setCellValue('A3', 'Cabang');
		$objPHPExcel->getActiveSheet()->setCellValue('B3', $cabang);
		//Set header
		$objPHPExcel->getActiveSheet()->setCellValue('A4', 'No');
		$objPHPExcel->getActiveSheet()->setCellValue('B4', 'Kasir');
		$objPHPExcel->getActiveSheet()->setCellValue('C4', 'Cabang Kasir');
		$objPHPExcel->getActiveSheet()->setCellValue('D4', 'Cabang Keberangkatan');
		$objPHPExcel->getActiveSheet()->setCellValue('E4', 'Tanggal');
		$objPHPExcel->getActiveSheet()->setCellValue('F4', 'No. Resi');
		$objPHPExcel->getActiveSheet()->setCellValue('G4', 'Jumlah');
		
		// Save it as an excel 2003 file
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$row = 5;
		
		for($i=0;$i<count($data['list_result']);$i++){
			$keberangkatan = '';
			for($j=0;$j<count($data['list_result'][$i]['cabang']);$j++){
				if ($j!=0)
					$keberangkatan.=', ';
				$keberangkatan.=$data['list_result'][$i]['cabang'][$j]['cabang_keberangkatan'];
			}
			
			
			$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, ($i+1));
			$objPHPExcel->getActiveSheet()->setCellValue('B'.$row, $data['list_result'][$i]['nama']);
			$objPHPExcel->getActiveSheet()->setCellValue('C'.$row, $data['list_result'][$i]['kode_cabang']);
			$objPHPExcel->getActiveSheet()->setCellValue('D'.$row, $keberangkatan);
			$objPHPExcel->getActiveSheet()->setCellValue('E'.$row, $data['list_result'][$i]['tanggal']);
			$objPHPExcel->getActiveSheet()->setCellValue('F'.$row, $data['list_result'][$i]['noresi']);
			$objPHPExcel->getActiveSheet()->setCellValue('G'.$row, 'Rp. '.number_format($data['list_result'][$i]['total'],0,'.',','));
			$row++;
		}
		$objWriter->save('public/Report Setoran Bank '.$tgl_awal.'-'.$tgl_akhir.'-'.$cabang.'.xls');
		redirect(PUBLIC_FOLDER.'Report Setoran Bank '.$tgl_awal.'-'.$tgl_akhir.'-'.$cabang.'.xls');
	}
	
}